package com.ruoyi.eam.service.impl;

import java.util.List;

import com.ruoyi.eam.doman.EamAsset;
import com.ruoyi.eam.doman.EamChgD;
import com.ruoyi.eam.mapper.EamAssetMapper;
import com.ruoyi.eam.mapper.EamChgHMapper;
import com.ruoyi.eam.mapper.EamLoanbackHMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eam.mapper.EamLoanbackDMapper;
import com.ruoyi.eam.doman.EamLoanbackD;
import com.ruoyi.eam.service.IEamLoanbackDService;

/**
 * 借用归还从Service业务层处理
 *
 * @author xl
 * @date 2024-07-08
 */
@Service
public class EamLoanbackDServiceImpl implements IEamLoanbackDService
{
    @Autowired
    private EamLoanbackDMapper eamLoanbackDMapper;
    @Autowired
    private EamLoanbackHMapper eamLoanbackHMapper;
    @Autowired
    private EamAssetMapper eamAssetMapper;

    /**
     * 查询借用归还从
     *
     * @param loanBackId 借用归还从主键
     * @return 借用归还从
     */
    @Override
    public EamLoanbackD selectEamLoanbackDByLoanBackId(Long loanBackId)
    {
        return eamLoanbackDMapper.selectEamLoanbackDByLoanBackId(loanBackId);
    }

    /**
     * 查询借用归还从列表
     *
     * @param eamLoanbackD 借用归还从
     * @return 借用归还从
     */
    @Override
    public List<EamLoanbackD> selectEamLoanbackDList(EamLoanbackD eamLoanbackD)
    {
        return eamLoanbackDMapper.selectEamLoanbackDList(eamLoanbackD);
    }

    /**
     * 新增借用归还从
     *
     * @param eamLoanbackD 借用归还从
     * @return 结果
     */
    @Override
    public int insertEamLoanbackD(List<EamAsset> eamAssets)
    {
        //获取最后一条变更id
        Long loanBackIdLast = eamLoanbackHMapper.queryEamLoanbackHList().get(eamLoanbackHMapper.queryEamLoanbackHList().size() - 1).getLoanBackId();
        for (EamAsset asset : eamAssets) {
            EamLoanbackD eamChgD = new EamLoanbackD();
            eamChgD.setLoanBackId(loanBackIdLast);
            eamChgD.setAssetId(asset.getAssetId());
            eamChgD.setCount(asset.getAssetNum());
            //添加变更从表信息
            int num = eamLoanbackDMapper.insertEamLoanbackD(eamChgD);
            //修改资产信息
            int num1 = eamAssetMapper.updateEamAsset(asset);
            if(num == 0 || num1 == 0){
                return 0;
            }
        }
        return 1;
    }

    /**
     * 修改借用归还从
     *
     * @param eamLoanbackD 借用归还从
     * @return 结果
     */
    @Override
    public int updateEamLoanbackD(EamLoanbackD eamLoanbackD)
    {
        return eamLoanbackDMapper.updateEamLoanbackD(eamLoanbackD);
    }

    /**
     * 批量删除借用归还从
     *
     * @param loanBackIds 需要删除的借用归还从主键
     * @return 结果
     */
    @Override
    public int deleteEamLoanbackDByLoanBackIds(Long[] loanBackIds)
    {
        return eamLoanbackDMapper.deleteEamLoanbackDByLoanBackIds(loanBackIds);
    }

    /**
     * 删除借用归还从信息
     *
     * @param loanBackId 借用归还从主键
     * @return 结果
     */
    @Override
    public int deleteEamLoanbackDByLoanBackId(Long loanBackId)
    {
        return eamLoanbackDMapper.deleteEamLoanbackDByLoanBackId(loanBackId);
    }

    @Override
    public int updateLoanBackD(List<EamAsset> eamAssets, Long loanBackId) {
        for (EamAsset asset : eamAssets) {
            EamLoanbackD eamChgD = new EamLoanbackD();
            eamChgD.setLoanBackId(loanBackId);
            eamChgD.setAssetId(asset.getAssetId());
            eamChgD.setCount(asset.getAssetNum());
            //添加变更从表信息
            int num = eamLoanbackDMapper.insertEamLoanbackD(eamChgD);
            //修改资产信息
            int num1 = eamAssetMapper.updateEamAsset(asset);
            if(num == 0 || num1 == 0){
                return 0;
            }
        }
        return 1;
    }
}
